<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="包装等级" prop="packGrade">
        <el-input
          v-model="queryParams.packGrade"
          placeholder="请输入包装等级"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入商品名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包装计量单位" prop="measurement">
        <el-select v-model="queryParams.measurement" placeholder="请选择包装计量单位" clearable size="small">
          <el-option
            v-for="dict in measurementOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="包装名称" prop="packName">
        <el-input
          v-model="queryParams.packName"
          placeholder="请输入包装名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="包装系数" prop="packModulus">
        <el-input
          v-model="queryParams.packModulus"
          placeholder="请输入包装系数"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['supplier:productPack:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['supplier:productPack:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['supplier:productPack:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['supplier:productPack:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="productPackList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="packId" />
      <el-table-column label="包装名称" align="center" prop="packName" />
      <el-table-column label="包装等级" align="center" prop="packGrade" />
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="包装计量单位" align="center" prop="measurement" :formatter="measurementFormat" />
      <el-table-column label="包装系数" align="center" prop="packModulus" />
      <el-table-column label="进货价格" align="center" prop="price" />
      <el-table-column label="零售价格" align="center" prop="marketPrice" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['supplier:productPack:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['supplier:productPack:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改包装对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="包装名称" prop="packName">
          <el-input v-model="form.packName" placeholder="请输入包装名称" />
        </el-form-item>
        <el-form-item label="包装等级" prop="packGrade">
          <el-input-number v-model="form.packGrade" placeholder="请输入包装等级" :min="1" :max="2" :step="1"/>
        </el-form-item>
        <el-form-item label="商品名称" prop="productName">
          <el-input v-model="form.productName" placeholder="请输入商品名称" >
            <el-button slot="append" icon="el-icon-search"  @click="handleProduct"  ></el-button>
          </el-input>
        </el-form-item>
        <el-form-item label="包装计量单位" prop="measurement">
          <el-select v-model="form.measurement" placeholder="请选择包装计量单位">
            <el-option
              v-for="dict in measurementOptions"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="parseInt(dict.dictValue)"
            ></el-option>
          </el-select>
        </el-form-item>
        
        <el-form-item label="包装系数" prop="packModulus">
          <el-input-number v-model="form.packModulus" placeholder="请输入包装系数" :step="1" :min="0"/>
        </el-form-item>
        <el-form-item label="进货价格" prop="price">
          <el-input-number v-model="form.price" placeholder="请输入进货价格" :precision="2" :step="0.1" :min="0"/>
        </el-form-item>
        <el-form-item label="零售价格" prop="marketPrice">
          <el-input-number v-model="form.marketPrice" placeholder="请输入零售价格" :precision="2" :step="0.1" :min="0"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!--选中商品组件-->
    <product-dialog
      :product-props.sync="productData"
      :handle-select="handleSelect"
      :radiobox="true"
      ref="productParams"
    />
  </div>
</template>

<script>
import { listProductPack, getProductPack, delProductPack, addProductPack, updateProductPack, exportProductPack } from "@/api/supplier/productPack";
import productDialog from "@/views/supplier/product/dialog";
export default {
  name: "ProductPack",
  components: {
    productDialog,
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 包装表格数据
      productPackList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 包装计量单位字典
      measurementOptions: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        packGrade: null,
        productName: null,
        measurement: null,
        packName: null,
        packModulus: null,
      },
      productData: {
        open: false,
        currentQtyShow: false,
        priceShow: true,
        marketPriceShow: true,
        productList: [],
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        packName: [
          {
            required: true,
            message: "包装名称不能为空",
          },
        ],
        productName: [
          {
            required: true,
            message: "商品名称不能为空",
          },
        ],
        packModulus: [
          {
            required: true,
            message: "包装系数不能为空",
          },
        ],
        measurement: [
          {
            required: true,
            message: "包装计量单位不能为空",
          },
        ],


        
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("bus_measurement").then(response => {
      this.measurementOptions = response.data;
    });
  },
  methods: {
    /** 查询包装列表 */
    getList() {
      this.loading = true;
      listProductPack(this.queryParams).then(response => {
        this.productPackList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 包装计量单位字典翻译
    measurementFormat(row, column) {
      return this.selectDictLabel(this.measurementOptions, row.measurement);
    },
    handleProduct(){
      this.$refs.productParams.open = true;
      this.$refs.productParams.getProductList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        packId: null,
        packGrade: null,
        productName: null,
        productId: null,
        measurement: null,
        packName: null,
        packModulus: null,
        price: null,
        marketPrice: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.packId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    handleSelect(data) {
      data.forEach((item) => {
        console.log("item",item)
        this.form.productName=item.productName;
        this.form.productId=item.productId;
        this.form.price=item.price;
        this.form.marketPrice =item.marketPrice;
      });
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加包装";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const packId = row.packId || this.ids
      getProductPack(packId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改包装";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.packId != null) {
            updateProductPack(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addProductPack(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const packIds = row.packId || this.ids;
      this.$confirm('是否确认删除包装编号为"' + packIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delProductPack(packIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有包装数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportProductPack(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
